package org.toonWatch

class User {

	transient springSecurityService

	String username
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
        String name
        String lastname
        String address1
        String address2
        String postal
        String phonenumber
        Date birthday
        BigDecimal voucherUsed = 0
        Boolean newsletter = false
        
    //only this line added( CHin Hui)    
    static hasMany = [voucher:Voucher]
          
    
	static constraints = {
		username blank: false, unique: true
		password blank: false
                name blank: false
                name blank: false, maxSize: 20
                lastname blank: false, maxSize: 20
                address1 blank: false
                postal blank: false, validator: RegisterController.postalValidator
                phonenumber blank: false, validator: RegisterController.phonenumberValidator
                birthday nullable: true;
                voucherUsed blank:true, nullable:true
               
	}


        
	static mapping = {
		password column: '`password`'
               
             
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
        
    String toString() {
        name
    }
}
